<?php

namespace App\Admin\Forms;

use App\Models\ExcelRule;
use Encore\Admin\Widgets\Form;
use Illuminate\Http\Request;

class Sheet4 extends Form
{
    /**
     * The form title.
     *
     * @var string
     */
    public $title = 'sheet4';

    public function title()
    {
        $request = \request();
        $params = $request->all();
        $tplId = $params['template_id'];
        $active = 'sheet4';
        $title = ExcelRule::query()->where('type', $active)->where('template_id', $tplId)->value('title');
        return $title ?: $active;
    }

    /**
     * Handle the form request.
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    /**
     * Handle the form request.
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request)
    {
        $params = $request->all();
        $active = $params['active'] ?? 'sheet4';

        $tplId = $params['template_id'] ?? 1;
        ExcelRule::query()->updateOrCreate([
            'template_id' => $tplId,
            'type'        => $active,
        ], [
            'config_json' => [
                'is_empty'    => $params['is_empty'] ?? '',
                'multi_data'  => $params['multi_data'] ?? [],
                'single_data' => $params['single_data'] ?? [],
            ]
        ]);

        admin_success("操作成功");

        return back();
    }

    /**
     * Build a form here.
     */
    public function form()
    {
        $request = \request();
        $params = $request->all();
        $active = $params['active'] ?? 'sheet1';
        $tplId = $params['template_id'] ?? 1;
        $this->hidden('template_id')->value($tplId);
        $this->hidden('active')->value($active);
        $this->table('single_data', __('固定信息'), function ($table) {
            $table->text('key', __('关键字'))->width("100px")->rules('required', [
                'required' => '请输入关键字',
            ]);
            $table->text('value', __('关键字表头'))->width("100px")->rules('required', [
                'required' => '请输入关键字表头',
            ])->placeholder("请输入表格表头");
           $table->text('desc', __('关键字描述'))->width("300px");
        });
        $this->text('is_empty',__("空数据过滤"))->placeholder("请与下方固定信息中字段保持一致");
        $this->table('multi_data', __('预约信息-多选'), function ($table) {
            $table->text('key', __('关键字'))->width("100px")->rules('required', [
                'required' => '请输入关键字',
            ]);
            $table->text('value', __('关键字表头'))->width("500px")->rules('required', [
                'required' => '请输入关键字表头',
            ])->placeholder("请输入表格表头，多个以英文逗号隔开。例：A,B");
           $table->text('desc', __('关键字描述'))->width("200px");
        });
    }

    /**
     * The data of the form.
     *
     * @return array $data
     */
    public function data()
    {
        $request = \request();
        $params = $request->all();
        $active = $params['active'] ?? 'sheet2';
        $tplId = $params['template_id'] ?? 1;
        $data = ExcelRule::query()->where('template_id', $tplId)->where('type', $active)->first();
        $configJson = $data['config_json'] ?? [];
        //var_dump($configJson['single_data']);die;
        return [
            'multi_data'  => $configJson['multi_data'] ?? [],
            'single_data' => $configJson['single_data'] ?? [],
            'is_empty'    => $configJson['is_empty'] ?? '',
            'template_id' => $tplId,
            'active'      => $active,
        ];
    }
}
